﻿using System;
using System.Collections.Generic;
using System.Text;
using AutoShutdownDotNet.Models;
using System.Diagnostics;

namespace AutoShutdownDotNet.Actions
{
    public class DoAction
    {

        public Task Task { get; set; }

        public DoAction(Task task)
        {
            Task = task;
        }

        public void Do()
        {
            bool succed = true;
            string error = string.Empty;
            try
            {
                switch (Task.Action)
                {
                    case AutoShutdownAction.Logoff:
                        SystemActions.Logoff();
                        break;
                    case AutoShutdownAction.Hibernate:
                        SystemActions.Hibernate();
                        break;
                    case AutoShutdownAction.PowerOff:
                        SystemActions.PowerOff();
                        break;
                    case AutoShutdownAction.Reboot:
                        SystemActions.Reboot();
                        break;
                    case AutoShutdownAction.RunApplication:
                        SystemActions.Run(Task.FilePath);
                        break;
                    case AutoShutdownAction.Standby:
                        SystemActions.Standby();
                        break;
                    case AutoShutdownAction.Lock:
                        SystemActions.Lock();
                        break;
                    case AutoShutdownAction.ShowMessage:
                        break;
                }
            }
            catch (Exception ex)
            {
                succed = false;
                error = ex.GetBaseException().Message;
            }
            
            History history = new History();
            history.Action = Task.Action;
            history.FilePath = Task.FilePath;
            history.Hour = Task.Hour;
            history.Minute = Task.Minute;
            history.When = DateTime.Now;
            history.Succeed = succed;
            history.Comment = error;

            history.Insert();
        }



    }
}
